<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0,user-scalable=0, width=device-width"/>
    <title>api</title>
    <link rel="stylesheet" type="text/css" href="../css/api.css" />
    <link rel="stylesheet" type="text/css" href="../css/common.css" />
    <link rel="stylesheet" type="text/css" href="../css/login.css" />
    <link rel="stylesheet" type="text/css" href="../css/base.css" />

</head>
    <style type="text/css">
    
        .m-user-bar .w-bar {
    color: #656565;
    font-size: 14px;
    
    
}
.w-bar {
    display: block;
    overflow: hidden;
    position: relative;
    padding: 0 14px;
    color: #525252;
    background: #fff;
}
.m-user-bar-avatar {

    line-height: 69px;
}
a {
    color: #0079fe;
    text-decoration: none;
    outline: none;
}

.m-user-bar .w-bar-ext {
    color: #A9A9A9;
}
.w-bar-ext {
    overflow: hidden;
    float: right;
    color: #a9a9a9;
}
.m-user-bar-avatar .m-user-avatar {
        height: 45px;
    margin: 12px 0;
    display: block;
}
span {
    display: block;
   
    /* display: inline-block; */
}
.m-user-bar .w-bar {
    color: #656565;
    font-size: 14px;
    
}
.dark, .m-user-link {
    line-height: 45px;
}
.w-bar {
    display: block;
    overflow: hidden;
    position: relative;
    padding: 0 14px;
    color: #525252;
    background: #fff;
}
.m-user-bar-avatar .m-user-avatar img {
    border-width: 0;
    border-radius: 35px;
}
.m-user-avatar img {
    border: 2px solid #FFF;
    border-radius: 2px;
}
.blue{
	color: #0079fe !important;
}

    </style>

</head>
<body>
    <header id='header'>
        <div class="header"> <i id='back' class="back" onclick="_back()"></i>
            <h1>个人资料</h1>
        </div>
    </header>
    <script id='userDetail-template' type="text/x-dot-template">
        <div class="m-user-info">
            <div class="m-user-bar m-user-bar-l">
                <a onclick='addImg()' class="m-user-bar-avatar w-bar border-bottom-img">
                    头像
                    <span class="w-bar-ext">
                        <span class="m-user-avatar">
                        {{if(isImg(it.img)){}}
         <img id='t_img' src="http://wap.jqmao.cn/statics/uploads/{{=it.img}}_160160.jpg" width='45px' height='45px'>
         {{}else if(isPng(it.img)){}}
         <img id='t_img' src="http://wap.jqmao.cn/statics/uploads/{{=it.img}}_160160.png" width='45px' height='45px'>
         {{}else{}}
         <img id='t_img' src="{{=it.img}}" width='45px' height='45px'>
         {{}}}
                            
                            </span>
                    </span>
                </a>

                <a href="javascript:void(0);" id="nickname" onclick="openWin('updateUsername',{{=it.uid}})" data-pro="nickname" class="w-bar dark border-bottom-img">
                    昵称
                    <span class="m-user-link w-bar-ext">
                        <span id='username'>{{=it.username}}</span>
                    </span>
                </a>
				{{if(it.mobile){}}
                    <a class="w-bar dark border-bottom-img" onclick="openHeader('updatemobile')">帐号
                        <span class="w-bar-ext blue" id='mobile'>{{=it.mobile}}</span>
                    </a>
                    {{}else{}}
                    <a class="w-bar dark border-bottom-img">账号
                        <span class="w-bar-ext">未绑定</span>
                    </a>
                {{}}}
                <a class="w-bar dark border-bottom-img">
                    ID
                    <span class="w-bar-ext">309{{=it.uid}}</span>
                </a>
                {{if(it.authorize&&it.authorize!=''){}}
                	<a class="w-bar dark border-bottom-img">第三方
                        <span class="w-bar-ext">{{=it.authorize}}</span>
                    </a>
               {{}}}
                    
                    
                
                <a class="w-bar dark border-bottom-img">
                    积分
                    <span class="w-bar-ext">{{=it.jingyan}}</span>
                </a>

                


            </div>
        </div>
    </script>
    <div class="content" id='user-detail'>
        
    </div>

</body>
    <script type="text/javascript" src="../script/zepto.min.js"></script>

    <script type="text/javascript" src="../script/api.js"></script>
    <script type="text/javascript" src="../script/doT.min.js"></script>
    <script type="text/javascript" src="../script/common.js"></script>
    <script type="text/javascript" src="../script/login.js"></script>
    <script type="text/javascript">
    var uid = null;
    apiready = function(){
    headerstatus()
    uid = $api.getStorage('user').uid;
    api.addEventListener({
    name:'keyback'
	}, function(ret, err){        
	     _back()
	});
    
   
 
    
    api.addEventListener({
    name: 'getnewusername'
		}, function(ret, err){
		
		var username_t = $api.byId('username');
		    var mobile_t = $api.byId('mobile');
		    if(ret.value.newmobile){
		    var _mphone =ret.value.newmobile.substr(3,4);
            var phonenum = ret.value.newmobile.replace(_mphone,"****");
         
		   		$api.html(mobile_t,phonenum);
		    }else if(ret.value.newusername){
		   		$api.html(username_t,ret.value.newusername);
		    }
		});
        getdetail()
    }
    function _back(){
    api.closeWin({
    	name:'orderContent'
    });
            api.historyBack({
            frameName:''
        },function(ret,err){
            if(!ret.status){
                api.execScript({
            name:'root',
            frameName: 'user',
            script: 'userReload()'
        });
                api.closeWin();
            }
        });
        }
        
       var rst = null;
        function getdetail(){
        
        user = $api.getStorage('user');
            var a =$api.byId('user-detail');
            var tpl = $api.byId('userDetail-template').text;
            var tempFn = doT.template(tpl);
            
            
            var mphone =user.mobile.substr(3,4);
            var lphone = user.mobile.replace(mphone,"****");
            user.mobile = lphone;
            $api.html(a,tempFn(user));
            }
            
            function addImg(){
		 api.confirm({
            title: '图片来源',
            msg: '您想要从哪里选取图片 ?',
            buttons:['相机', '相册', '取消']
        },function(ret,err){
            var sourceType = 'album';

            if(3 == ret.buttonIndex){ // 取消
                return;
            }

            if(1 == ret.buttonIndex){ // 打开相机
                sourceType = 'camera';
            }

            api.getPicture({
                sourceType: sourceType,
                encodingType: 'jpg',
                mediaValue: 'pic',                
                destinationType: 'url',
                 targetWidth: 120,
    			targetHeight: 120,
            }, function(ret, err){
           
                if (ret.data) {
                
               //alert(JSON.stringify(ret))
                    var f1 = $api.byId('t_img');
                    
			        $api.attr(f1,"src",''+ret.data); 
			       if(ret.data){
			        //alert(JSON.stringify(ret.data))
			      	 	//index+=1;
                    	//var i = index-1;
                    	rst = ret.data;   
                    	
                    	save()
                    	
			       }
                    

                }
            });

        });
			
		}
		function save(){
		
			
			//hasSubmit = true;	
			
			
       		api.ajax({
        	url: 'http://wap.jqmao.cn/UploadAvatar',
        	method: 'post',
        	data: {
           	 		values: {uid:uid},
           	 		files: { 
            		"file[]":rst
        	}
       		}
       					
        	
    		}, function (ret, err) {
    		if(ret){
					if(ret.code==0){
						toast("亲，上传成功");
					}
						
						
						//openWin('myShare',uid);
					
			}else{
				//alert(JSON.stringify(err))
			}
		
		})
		}
  
    </script>

</html>
